package com.ruoyi.project.jxc.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.project.jxc.domain.JxcDrug;
import com.ruoyi.project.jxc.domain.JxcMf;
import com.ruoyi.project.jxc.mapper.JxcDrugMapper;
import com.ruoyi.project.jxc.mapper.JxcMfMapper;
import com.ruoyi.project.jxc.service.IJxcDrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JxcDrugServiceImpl implements IJxcDrugService {
    @Autowired
    private JxcDrugMapper jxcDrugMapper;
    @Autowired
    private JxcMfMapper mfMapper;
    @Override
    public List<JxcDrug> selectDrugList(JxcDrug drug) {
        List<JxcDrug> jxcDrugs = jxcDrugMapper.selectDrugList(drug);
        for (JxcDrug d:jxcDrugs
             ) {
            JxcMf jxcMf = mfMapper.selectMfById(d.getMfId());
            d.setMf(jxcMf);
        }
        return jxcDrugs;
    }

    @Override
    public String checkDrugNameUnique(JxcDrug drug) {
        JxcDrug jxcDrug = jxcDrugMapper.checkDrugNameUnique(drug.getDrugName());
        if(drug.equals(jxcDrug)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
        }

    @Override
    public int insertDrug(JxcDrug drug) {
        return jxcDrugMapper.insertDrug(drug);
    }

    @Override
    public int updateDrug(JxcDrug drug) {
        return jxcDrugMapper.updateDrug(drug);
    }

    @Override
    public JxcDrug selectDrugById(Long drugId) {
        JxcDrug jxcDrug = jxcDrugMapper.selectDrugById(drugId);
        JxcMf jxcMf = mfMapper.selectMfById(jxcDrug.getMfId());
        jxcDrug.setMf(jxcMf);
        return jxcDrug;
    }

    @Override
    public int deleteDrugById(Long[] drugId) {
        return jxcDrugMapper.deleteDrugById(drugId);
    }
}

